﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Collections;
using System.Text;
using System.Windows.Forms;

namespace Tab2Sql.Forms
{
    /// <summary>
    ///
    /// </summary>
    public partial class SettingsPanel : Form
    {
        /// <summary>
        ///
        /// </summary>
        public SettingsPanel()
        {
            InitializeComponent();
            moo();
        }


        private void moo()
        {
            boxAssemblyVersion.Text = Assemblage.AssemblyVersionString();
            IEnumerator en = Properties.Settings.Default.Properties.GetEnumerator();
            ComboBox x = new ComboBox();
            while (en.MoveNext())
            {
                System.Configuration.SettingsProperty setting = (System.Configuration.SettingsProperty)en.Current;
            

                comboBox1.Items.Add(setting.Name);

                //listSettings.Items.Add(setting.Name);
            }
            comboBox1.Sorted = true;
            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string choice = comboBox1.SelectedItem.ToString();
            settingLabel.Text = choice;
            textBox1.Text = Properties.Settings.Default[choice].ToString();
            textBox1.Refresh();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string newSetting = textBox2.Text;
            if (textBox2.Text.Length == 0 || textBox2.Text == textBox1.Text)
            {
                MessageBox.Show("No action required.");
                    return;
            }
            string choice = comboBox1.SelectedItem.ToString();

            trySave(newSetting, choice);

        }

        private void trySave(string newSetting, string choice)
        {
            if (Properties.Settings.Default[choice].GetType().ToString() == "System.Int32")
            {
                try
                {
                    Properties.Settings.Default[choice] = Convert.ToInt32(newSetting);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            if (Properties.Settings.Default[choice].GetType().ToString() == "System.DateTime")
            {
                try
                {
                    Properties.Settings.Default[choice] = DateTime.Parse(newSetting);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else if (Properties.Settings.Default[choice].GetType().ToString() == "System.String")
            {
                Properties.Settings.Default[choice] = newSetting;
            }
            Assemblage asm = new Assemblage();
            Properties.Settings.Default.Assembly_String = asm.AssemblyVersion;
            asm = null;

            Properties.Settings.Default.Save();

            textBox1.Text = Properties.Settings.Default[choice].ToString();
            textBox1.Refresh();
        }

        private void btnSaveRestart_Click(object sender, EventArgs e)
        {
            string newSetting = textBox2.Text;
            if (newSetting.Trim().Length > 0)
            {
                string choice = comboBox1.SelectedItem.ToString();
                trySave(newSetting, choice);
            }

            Application.Restart();
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
